﻿#include "opentreethread.h"
#include <QDir>
#include "protreeitem.h"
#include "const.h"
#include <QDebug>


OpenTreeThread::OpenTreeThread(const QString &src_path, int file_count, QTreeWidget *self,
                               QObject *parent)
    :QThread(parent),_bstop(false),_src_path(src_path),_file_count(file_count),
    _self(self),_root(nullptr)
{

}

void OpenTreeThread::OpenProTree(const QString &src_path, int &file_count, QTreeWidget *self)
{
    QDir src_dir(src_path);
    auto name = src_dir.dirName();
    auto * item = new ProTreeItem(self,name,src_path,TreeItemPro);

    item->setData(0,Qt::DisplayRole,name);
    item->setData(0,Qt::DecorationRole,QIcon(":/icon/dir.png"));
    item->setData(0,Qt::ToolTipRole,src_path);
    _root = item;
    //递归调用
    RecursiveProTree(src_path,file_count,self,_root,item,nullptr);

}

void OpenTreeThread::run()
{
    OpenProTree(_src_path, _file_count, _self);

    //取消操作，删除目录树，清掉内存
    if(_bstop)
    {
        auto path = dynamic_cast<ProTreeItem*>(_root)->GetPath();
        auto index = _self->indexOfTopLevelItem(_root);
        delete _self->takeTopLevelItem(index);
        QDir dir(path);
        dir.removeRecursively();

        return;
    }

    emit SigFinishProgress(_file_count);
}

void OpenTreeThread::RecursiveProTree(const QString &src_path, int &file_count, QTreeWidget *self, QTreeWidgetItem *root, QTreeWidgetItem *parent, QTreeWidgetItem *preitem)
{
    QDir src_dir(src_path);
    qDebug() << "src_path is " << src_path << "\n";

    src_dir.setFilter(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot);//除了目录或文件，其他的过滤掉
    src_dir.setSorting(QDir::Name);//优先显示名字

    //逻辑实现
    QFileInfoList list = src_dir.entryInfoList();
    qDebug() << "list.size " << list.size() << "\n";
    for(int i = 0; i<list.size(); i++)
    {
        if(_bstop) return;

        QFileInfo fileInfo = list.at(i);
        if(fileInfo.isDir())
        {
            //如果是文件夹
            if(_bstop) return;

            file_count++;
            emit SigUpdateProgress(file_count);

            QString sub_dist_path = src_dir.absoluteFilePath(fileInfo.fileName());
            QDir sub_dist_dir(sub_dist_path);

            if(!sub_dist_dir.exists())
            {
                //子文件夹不存在
                bool ok = sub_dist_dir.mkpath(sub_dist_path);
                if(!ok) continue;
            }

            //成功则创建item
            auto *item = new ProTreeItem(_root,fileInfo.fileName(),fileInfo.absoluteFilePath(),
                                         _root,TreeItemDir);
            item->setData(0,Qt::DisplayRole,fileInfo.fileName());
            item->setData(0,Qt::DecorationRole,QIcon(":/icon/dir.png"));
            item->setData(0,Qt::ToolTipRole,fileInfo.absoluteFilePath());

            //递归调用
            RecursiveProTree(fileInfo.absoluteFilePath(),file_count,self,root,item,preitem);

        }else
        {
            //如果是文件
            if(_bstop) return;

            const QString& suffix = fileInfo.completeSuffix();
            if(suffix != "png" && suffix != "jpeg" && suffix != "jpg")
            {
                qDebug() << "suffix is not pic " << suffix << "\n";
                continue;
            }

            file_count++;
            emit SigUpdateProgress(file_count);

            //成功则创建item
            auto *item = new ProTreeItem(_root,fileInfo.fileName(),fileInfo.absoluteFilePath(),
                                         _root,TreeItemPic);

            //成功则创建item
            item->setData(0,Qt::DisplayRole,fileInfo.fileName());
            item->setData(0,Qt::DecorationRole,QIcon(":/icon/pic.png"));
            item->setData(0,Qt::ToolTipRole,fileInfo.absoluteFilePath());
            if(preitem)
            {
                auto * pre_proitem = dynamic_cast<ProTreeItem*>(preitem);
                pre_proitem->SetNextItem(item);
            }

            item->SetPreItem(preitem);
            preitem = item;
        }
    }

    emit SigFinishProgress(file_count);
}

void OpenTreeThread::SlotCancelProgress()
{
    this->_bstop = true;
}

